package com.credithc.signature.job.service;

import com.credithc.signature.common.mapper.RequestLogMapper;
import com.credithc.signature.common.po.Channel;
import com.credithc.signature.common.po.Template;
import com.credithc.signature.common.service.ChannelService;
import com.credithc.signature.common.service.TemplateService;
import com.credithc.signature.common.vo.manager.RequestLogVo;
import com.credithc.signature.job.client.QyWxClient;
import com.credithc.signature.job.ro.EmailRo;
import com.credithc.signature.job.ro.WxRo;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.TIMEOUT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc: <br/>
 * @author: Biyun Fu<br/>
 * @time：2021年06⽉28⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
@Service
public class StatisticsReqService {

    @Autowired
    private RequestLogMapper requestLogMapper;

    @Autowired
    ChannelService channelService;

    @Autowired
    TemplateService templateService;

    @Autowired
    private QyWxClient client;

    @Value("${qywx.key}")
    private String key;

    @Value("${alarm.num}")
    private String num;

    @Value("${alarm.email}")
    private List<String> email;

    @Value("${alarm.time}")
    private Integer time;

    @Value("${alarm.env}")
    private String env;

    /**
     * 定时任务统计失败合同
     * @return
     */
    public void statisticsRequestFail(String startTime, String endTime) {
        List<RequestLogVo> list = requestLogMapper.StatisticsRequestLog(startTime, endTime, 3, null);
        log.info("开始执行【签章失败统计】失败数：{}", list);
        if (list != null && list.size() > 0) {
            int sum = 0;
            for (RequestLogVo vo:
                    list) {
                sum = sum + vo.getFailCallNum();
            }
            log.info("开始执行【签章失败统计】失败总数：{}", sum);
            if (sum > Integer.parseInt(num)) {
                //报警
                alarm(list,startTime,endTime,"【签章失败报警】 环境：" + env + " ");
            }
        }
    }

    /**
     * 定时任务统计超时合同
     * @return
     */
    public void statisticsRequestTime(String startTime, String endTime) {
        List<RequestLogVo> list = requestLogMapper.StatisticsRequestLog(startTime,endTime,2,time);
        log.info("开始执行【签章超时统计】超时数：{}", list);
        if (list != null && list.size() > 0) {
            int sum = list.stream().mapToInt(RequestLogVo::getFailCallNum).sum();
            log.info("开始执行【签章超时统计】超时总数：{}", sum);
            if (sum > Integer.parseInt(num)) {
                //报警
                alarm(list,startTime,endTime,"【签章超时报警】 环境：" + env + " ");
            }
        }
    }

    /**
     * 定时任务统计当天各业务系统
     * @return
     */
    public void statisticsRequestDayFail(String startTime, String endTime) {
        List<RequestLogVo> list = requestLogMapper.StatisticsRequestLog(startTime,endTime,3,null);
        if (list != null && list.size() > 0) {
            //报警
            alarm(list,startTime,endTime,"【签章昨日失败统计】 环境：" + env + " ");
        }
    }


    public void send(String str,String title) {
        //String[] arr = new String[]{email};
        //WxRo wxRo = new WxRo(key,"test","\"<div class=\\\"gray\\\">签章报警</div> <div class=\\\"normal\\\">详细信息</div><div class=\\\"highlight\\\">"+str+"</div>\"","url" , Arrays.asList(arr));
        //企业微信
        //client.sendWxText(wxRo);
        //client.sendWxCard(wxRo);
        //企业邮件
        EmailRo emailRo = new EmailRo(key,title,str,email);
        String data = client.sendEmail(emailRo);
        log.info("发送结果-------------"+data);
    }

    public void alarm(List<RequestLogVo> list,String startTime, String endTime, String title) {
        try {
            //报警
            log.info("开始拼接报警数据");
            StringBuilder data = new StringBuilder();
            data.append("<div>\n<p style=\"font-size: 14px; color: #464646;\">" + title + "统计时间区间：");
            data.append(startTime);
            data.append("  至  ");
            data.append(endTime);
            data.append("</p >\n");
            data.append("<table style=\"border-collapse: collapse;border-spacing: 0;\">\n");
            data.append("<tr style=\"background: #eef3f9;\">\n");
            data.append("<th style=\"border: 1px solid #000; padding: 5px;\">渠道号</th>\n");
            data.append("<th style=\"border: 1px solid #000; padding: 5px;\">渠道名称</th>\n");
            data.append("<th style=\"border: 1px solid #000; padding: 5px;\">模板号</th>\n");
            data.append("<th style=\"border: 1px solid #000; padding: 5px;\">数量</th>\n");
            data.append("</tr>\n");
            for (RequestLogVo vo : list) {
                String templateNo = vo.getTemplateNo();
                String channelNo = vo.getChannelNo();
                Channel channel = channelService.selectByChannelNo(channelNo);
                Template template = templateService.selectByTemplateNo(templateNo);
                data.append("<tr>\n");
                data.append("<td style=\"border: 1px solid #000; padding: 5px;\">" + channel.getChannelNo() + "</td>\n");
                data.append("<td style=\"border: 1px solid #000; padding: 5px;\">" + channel.getChannelName() + "</td>\n");
                data.append("<td style=\"border: 1px solid #000; padding: 5px;\">" + template.getTemplateNo() + "</td>\n");
                data.append("<td style=\"border: 1px solid #000; padding: 5px;\">" + vo.getFailCallNum() + "</td>\n");
                data.append("</tr>\n");
            }
            data.append("</table>\n");
            data.append("</div>");
            send(data.toString(), title);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
